<?php

    namespace Logistics\Controller;

    use Common\Controller\AdminbaseController;

    class AdminBillController extends AdminbaseController {

        protected $m_logistics_bill,$m_card,$m_logistics,$m_sale;

        function _initialize() {
            parent::_initialize();
            $this->m_logistics_bill= D("Logistics/LogisticsBill");
            $this->m_card = D("Admin/Card");
            $this->m_logistics = D("Admin/Logistics");
            $this->m_sale = D("Admin/Sale");
            $this->m_sale_manager = D("Admin/SaleManager");
        }

        /**
         *会员卡结账实时统计
         */
        public  function operation_daily(){
            $card_id_list =get_card_id_by_logistics();
            $where = array();
            //上级消费机构筛选
            $s_smid = I("s_smid");
            $this->assign("s_smid",$s_smid);
            if(!empty($s_smid)){
                $where["c.sale_manager_id"] = $s_smid;
            }
            //二级消费机构筛选
            $s_sid = I("s_sid");
            $this->assign("s_sid",$s_sid);
            if(!empty($s_sid)){
                $where["c.sale_id"] = $s_sid;
            }
            //二级物流公司筛选
            $s_lid = I("s_lid");
            $this->assign("s_lid",$s_lid);
            if(!empty($s_lid)){
                $where["c.lid"] = $s_lid;
            }
            //上级级物流公司筛选
            $s_mlid = I("s_mlid");
            $this->assign("s_mlid",$s_mlid);
            if(!empty($s_mlid)){
                $lid_child = M("Logistics")->where(array("manager_id"=>$s_mlid))->getField();
                $lid_child = $lid_child?explode(',', $lid_child):array();
                $lid_child[] = $s_mlid;
                $lid_child = implode(',', $lid_child);
                $where["c.lid"] = array('in',$lid_child);
            }
            //时间筛选
            $s_start_time = I("s_start_time");
            $s_end_time = I("s_end_time");
            if(empty($s_start_time)){
                $s_start_time = strtotime(date("Y-m-d 00:00:00"));
            }else{
                $s_start_time = strtotime($s_start_time.":00");
            }
            if(empty($s_end_time)){
                $s_end_time = time();
            }else{
                $s_end_time = strtotime($s_end_time.":59");
            }
            $this->assign("s_start_time",$s_start_time);
            $this->assign("s_end_time",$s_end_time);
            $where["c.create_time"]=array("BETWEEN",array($s_start_time,$s_end_time));


            if(!empty($card_id_list)){
                $where["c.cid"] = array("in",$card_id_list);
            }elseif(!(is_manager($this->user_model))){
                $where["c.cid"] = "abcd";
            }

            // $count = M("CardSale")->alias("c")->where($where)->count();
            // $page = $this->page($count, 15);

            $list = M("CardSale")->field("c.*,s.sale_name,sm.sale_manage_name,l.logistics_name")->alias("c")
                ->join("bdt_sale s ON s.id = c.sale_id")
                ->join("bdt_sale_manager sm ON sm.id = c.sale_manager_id","LEFT")
                ->join("bdt_logistics l ON l.id = c.lid","LEFT")
                ->where($where)
                // ->limit($page->firstRow , $page->listRows)
                ->order("c.create_time DESC")->select();
            $result = array();
            foreach($list AS $litem){
                $key = date("Ymd",$litem['create_time']).$litem['sale_id'];
                if(empty($result[$key])){
                    $result[$key]['time'] = date("Y-m-d",$litem["create_time"]);
                    $result[$key]['sale_name'] =$litem['sale_name'];
                    $result[$key]['sale_manage_name'] =$litem['sale_manage_name'];
                    $result[$key]['logistics_name'] =$litem['logistics_name'];
                    $result[$key]['sale_num'] = 1;
                    if ($litem['is_oil'] == 1) {
                        $result[$key]['sale_oil_num'] = $litem['goods_num'];
                        $result[$key]['sale_nooil_num'] = 0;
                    }else{
                        $result[$key]['sale_oil_num'] = 0;
                        $result[$key]['sale_nooil_num'] = $litem['goods_num'];
                    }
                    $result[$key]['card_num'][] = $litem['cid'];
                    $result[$key]['price'] = $litem['price'];
                    $result[$key]['point'] = $litem['general_point']+$litem['self_point'];
                }else{
                    $result[$key]['sale_num']++;
                    if ($litem['is_oil'] == 1) {
                        $result[$key]['sale_oil_num']+=$litem['goods_num'];
                    }else{
                        $result[$key]['sale_nooil_num']+=$litem['goods_num'];
                    }
                    if(!in_array($litem['cid'],$result[$key]['card_num'])){
                        $result[$key]['card_num'][] = $litem['cid'];
                    }
                    $result[$key]['price'] = $result[$key]['price']+$litem['price'];
                    $result[$key]['point'] = $result[$key]['point']+ $litem['general_point']+$litem['self_point'];
                }
            }

            foreach($result AS &$ritem){
                $ritem["card_number"] = count($ritem["card_num"]);
            }

            if(I("s_download") == 1){
                $export_title = array(
                    'time'=>'日期',
                    'sale_name'=>'机构名称',
                    'sale_num'=>'销售笔数',
                    'card_number'=>'卡片数',
                    'price'=>'支付金额',
                    'point'=>'赠送积分',
                );
                $excel = new \Common\Lib\excel();
                $excel->export_data('会员卡结账实时统计',$export_title,$result);exit;
            }

            $sale_list = $this->m_sale->select();
            $this->assign("sale_list",$sale_list);
            $sale_manager_list = $this->m_sale_manager->select();
            $this->assign("sale_manager_list",$sale_manager_list);
            $logistics_list = $this->m_logistics->where(array('manager_id'=>array('neq','0')))->select();
            $this->assign("logistics_list",$logistics_list);
            $logistics_manager_list = $this->m_logistics->where(array('manager_id'=>array('eq','0')))->select();
            $this->assign("logistics_manager_list",$logistics_manager_list);

            /*汇总*/
            $summary = array(
                'count'=>0,
                'goods_price'=>0,
                'dis_price'=>0,
                'price'=>0,
                'goods_num'=>0,
                'oil_num'=>0,
                'not_oil_num'=>0,
            );
            foreach ($list as $_key => $_row) {
                $summary['count'] += 1;
                $summary['goods_price'] += $_row['goods_price'];
                $summary['dis_price'] += $_row['dis_price'];
                $summary['price'] += $_row['price'];
                $summary['goods_num'] += $_row['goods_num'];
                if ($_row['is_oil'] == '1') {
                    $summary['oil_num'] += $_row['goods_num'];
                }else{
                    $summary['not_oil_num'] += $_row['goods_num'];
                }
            }
            $this->assign("summary",$summary);

            $this->assign("list",$result);
            $this->display();
        }

        /**
         * 会员卡充值实时统计
         */
        public function recharge_daily(){
            $card_id_list =get_card_id_by_logistics();


            //时间筛选
            $s_start_time = I("s_start_time");
            $s_end_time = I("s_end_time");
            if(empty($s_start_time)){
                $s_start_time = strtotime(date("Y-m-d 00:00:00"));
            }else{
                $s_start_time = strtotime($s_start_time.":00");
            }
            if(empty($s_end_time)){
                $s_end_time = time();
            }else{
                $s_end_time = strtotime($s_end_time.":59");
            }
            $this->assign("s_start_time",$s_start_time);
            $this->assign("s_end_time",$s_end_time);

            $where = array(
                "r.model"=>2,
                'r.create_time'=>array("BETWEEN",array($s_start_time,$s_end_time)),
            );
            $s_rid = I("s_rid");
            $this->assign("s_rid",$s_rid);
            if(!empty($s_rid)){
                $where["r.lid"] = $s_rid;
            }

            if(!empty($card_id_list)){
                $where["r.rid"] = array("IN",$card_id_list);
            }elseif(!(is_manager($this->user_model))){
                $where["r.rid"] = "abcd";
            }

            $list =  M("Recharge")->alias('r')
                ->where($where)
                ->join('bdt_logistics l ON l.id = r.lid',"LEFT")
                ->field("r.*,l.logistics_name")
                ->order("r.create_time DESC")->select();
            $result = array();
            foreach($list AS $litem){
                $key = date("Ymd",$litem['create_time']).$litem["lid"];
                if(empty($result[$key])){
                    $result[$key]['time'] = date("Y-m-d",$litem['create_time']);
                    $result[$key]['logistics_name'] =$litem['logistics_name'];
                    $result[$key]['price'] = $litem['price'];
                    $result[$key]['give_price'] = $litem['general_price']+$litem['self_price'];
                    $result[$key]['sale_num'] = 1;
                    $result[$key]['card_num'][] = $litem['rid'];

                }else{
                    $result[$key]['sale_num']++;
                    if(!in_array($litem['rid'],$result[$key]['card_num'])){
                        $result[$key]['card_num'][] = $litem['rid'];
                    }
                    $result[$key]['price'] = $result[$key]['price']+$litem['price'];
                    $result[$key]['give_price'] = $result[$key]['give_price']+ $litem['general_price']+$litem['self_price'];
                }
            }

            foreach($result AS &$ritem){
                $ritem["card_nummber"] = count($ritem["card_num"]);
            }

            if(I("s_download") == 1){
                $export_title = array(
                    'time'=>'日期',
                    'logistics_name'=>'机构名称',
                    'price'=>'充值金额',
                    'give_price'=>'赠送金额',
                    'sale_num'=>'充值笔数',
                    'card_nummber'=>'充值卡片数',
                );
                $excel = new \Common\Lib\excel();
                $excel->export_data('会员卡充值实时统计',$export_title,$result);exit;
            }

            $logistics_list = $this->m_logistics->get_list();
            $this->assign("logistics_list",$logistics_list);
            $this->assign("list",$result);
            $this->display();
        }

        /**
         * 机构实时余额
         */
        public function balance(){
            $list = array();
            if(is_manager($this->user_model)){
                $list = M("Logistics")->select();
            }else{
                $list[] = get_logistics_data();
                $lid = get_logistics_id();
                $lid_child = M("Logistics")->where(array("manager_id"=>$lid))->select();
                if(!empty($lid_child)) foreach ($lid_child as $_key => &$_row) {
                    $_row['logistics_name'] = '└── '.$_row['logistics_name'];
                    $list[] = $_row;
                }

            }

            $this->assign("list",$list);
            $this->display();
        }

        /**
         * 物流公司充值统计报表
         */
        public function recharge_report(){
            $user_model = $this->user_model;
            $type = I("request.type");  //等于1  日报 等于2  月报
            if(empty($type)){
                $type = 1;
            }
            $this->assign("type",$type);
            if(is_manager($this->user_model)){
                $s_rid = I("POST.s_rid");
                $this->assign("s_rid",$s_rid);

                if(!empty($s_rid)){
                    $where = array(
                        'b.lid'=>$s_rid
                    );
                }else{
                    $where = array();
                }

            }else{
                $where = array(
                    'b.lid'=> array('in', get_logistics_ids())
                );
            }

            $this->assign("user_model",$user_model);
            $logistics_list = $this->m_logistics->get_list();
            $this->assign("logistics_list",$logistics_list);
            //时间
            if($type == 1){
                $s_start_time = I("s_start_time");
                $s_end_time = I("s_end_time");
                if(empty($s_start_time)){
                    $s_start_time = strtotime(date("Y-m-d 00:00:00")." -7 day");
                }else{
                    $s_start_time = strtotime($s_start_time.":00");
                }
                if(empty($s_end_time)){
                    $s_end_time = time();
                }else{
                    $s_end_time = strtotime($s_end_time.":59");
                }
                $this->assign("s_start_time",$s_start_time);
                $this->assign("s_end_time",$s_end_time);
                $where["b.create_time"]=array("BETWEEN",array($s_start_time,$s_end_time));
            }

            //是否导出
            $s_download = I("s_download");

            if($s_download ==1){
                $list = M("LogisticsBill")->alias("b")->field("b.*,l.logistics_name")->where($where)
                    ->join("bdt_logistics l ON l.id = b.lid")
                    ->order("id DESC")
                    ->select();
            }else{
                $count= M("LogisticsBill")->alias("b")->where($where)->count();
                // $page = $this->page($count, 15);
                $list = M("LogisticsBill")->alias("b")->field("b.*,l.logistics_name")->where($where)
                    ->join("bdt_logistics l ON l.id = b.lid")
                    ->order("id DESC")
                    // ->limit($page->firstRow , $page->listRows)
                    ->select();
            }

            $result = array();
            if($type == 1){
                $excel_name = "物流公司日充值统计报表";
                foreach($list as $litem){
                    $key = date("Ymd",$litem['create_time']).$litem['lid'];
                    if(empty($result[$key])){
                        $result[$key]['balance'] = $litem['balance'];
                        $result[$key]['logistics_name'] = $litem['logistics_name'];
                        $result[$key]['time'] = date("Y-m-d",$litem['create_time']);
                        $result[$key][$litem['model']] = $litem['price'];
                    }else{
                        $result[$key][$litem['model']] = $result[$key][$litem['model']]+$litem['price'];
                    }
                }
            }else{
                $excel_name = "物流公司月充值统计报表";
                foreach($list AS $litem){
                    $key = date("Ym".$litem['lid'],$litem['create_time']);
                    if(empty($result[$key])){
                        $result[$key]['balance'] = $litem['balance'];
                        $result[$key]['logistics_name'] = $litem['logistics_name'];
                        $result[$key]['time'] = date("Y年m月",$litem['create_time']);
                        $result[$key][$litem['model']] = $litem['price'];
                    }else{
                        $result[$key][$litem['model']] = $result[$key][$litem['model']]+$litem['price'];
                    }

                }
            }


            //设置导出数据
            $export_title = array(
                "time"=>"日期",
                "logistics_name"=>"机构名称",
                "2"=>"账户充值",
                "3"=>"会员卡充值",
                "1"=>"授信总额",
                "4"=>"开卡费",
                "5"=>"账户充值赠送",
                "6"=>"卡片充值赠送",
                "7"=>"退款金额",
                "balance"=>"余额",
            );

            if($s_download == 1){
                $excel = new \Common\Lib\excel();
                $excel->export_data($excel_name,$export_title,$result);exit;
            }

            $this->assign("type",$type);
            $this->assign("list",$result);
            $this->display();
        }

        /**
         * 机构发卡统计日报
         */
        public function card_report(){

            if(is_manager($this->user_model)){
                $where = array();
            }else{
                $where['id'] = array('in',get_logistics_ids());
            }
            $list = M("Logistics")->where($where)->select();
            $this->assign("list",$list);
            $this->display();
        }

        /**
         * 物流公司统计报表
         */
//        public function index(){
//            $count=$this->m_driver->count();
//            $page = $this->page($count, 15);
//            $list = $this->m_driver->field("d.*,l.logistics_name")->alias("d")
//                ->join("bdt_logistics l ON l.id = d.lid")
//                ->order("create_time desc")
//                ->limit($page->firstRow , $page->listRows)
//                ->select();
//            $this->assign("page", $page->show('Admin'));
//            $this->assign("list",$list);
//            $this->display();
//        }

    }